package mms

class MedicineCompanyController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        params.sort = "id"
        params.order ="desc"
        if (params.keyword) { // encodeAsHTML

                 params.keyword = params.keyword.decodeURL()
            
             [medicineCompanyInstanceList: MedicineCompany.findAllByNameIlike("%${params.keyword}%" ,params), 
                medicineCompanyInstanceTotal: MedicineCompany.countByNameIlike("%${params.keyword}%") ,
                keyword : params.keyword]
        }else{
            [medicineCompanyInstanceList: MedicineCompany.list(params), 
                medicineCompanyInstanceTotal: MedicineCompany.count()]
        }
    }

    def realdata = {
        println "==========================${params.id}"
        def medicineCompanyInstance = MedicineCompany.get(params.id)
        if (medicineCompanyInstance) {
            params.order = "desc"
            params.sort = "dateCreated"
            params.max = 10;
            [medicineRetailInstanceList:MedicineRetail.findAllByCompany(medicineCompanyInstance, params),
                        medicineRetailInstanceTotal: MedicineRetail.countByCompany(medicineCompanyInstance),
                        id: params.id]

        }else {
              render("暂时没有数据！") 
        }
    }

    def allpic = {
        def medicineCompanyInstance = MedicineCompany.get(params.id)
        if (medicineCompanyInstance) {
            return [multimedia : medicineCompanyInstance.multimedia]
        }else{
            render("暂时没有数据！") 
        }
    }

    def movie = {
        render("暂时没有数据！")
    }



    def create = {
        def medicineCompanyInstance = new MedicineCompany()
        medicineCompanyInstance.properties = params
        return [medicineCompanyInstance: medicineCompanyInstance]
    }

    def save = {
        def medicineCompanyInstance = new MedicineCompany(params)

        log.info medicineCompanyInstance.quality 
        if (medicineCompanyInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'medicineCompany.label', default: 'MedicineCompany'), medicineCompanyInstance])}"
            //redirect(action: "list")
            redirect(action: "editgis", id: medicineCompanyInstance.id)
        }
        else {
            render(view: "create", model: [medicineCompanyInstance: medicineCompanyInstance])
        }
    }

    def editgis = {
        def medicineCompanyInstance = MedicineCompany.get(params.id)
        if (!medicineCompanyInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'medicineCompany.label', default: 'MedicineCompany'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [medicineCompanyInstance: medicineCompanyInstance]
        }
    }

    def show = {
        def medicineCompanyInstance = MedicineCompany.get(params.id)
        if (!medicineCompanyInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'medicineCompany.label', default: 'MedicineCompany'), params.id])}"
            redirect(action: "list")
        }
        else {
            [medicineCompanyInstance: medicineCompanyInstance]
        }
    }

    def edit = {
        def medicineCompanyInstance = MedicineCompany.get(params.id)
        if (!medicineCompanyInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'medicineCompany.label', default: 'MedicineCompany'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [medicineCompanyInstance: medicineCompanyInstance]
        }
    }

    def update = {
        def medicineCompanyInstance = MedicineCompany.get(params.id)
        if (medicineCompanyInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (medicineCompanyInstance.version > version) {
                    
                    medicineCompanyInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'medicineCompany.label', default: 'MedicineCompany')] as Object[], "Another user has updated this MedicineCompany while you were editing")
                    render(view: "edit", model: [medicineCompanyInstance: medicineCompanyInstance])
                    return
                }
            }
            medicineCompanyInstance.properties = params
//            medicineCompanyInstance.

            if (!medicineCompanyInstance.hasErrors() && medicineCompanyInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'medicineCompany.label', default: 'MedicineCompany'), medicineCompanyInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [medicineCompanyInstance: medicineCompanyInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'medicineCompany.label', default: 'MedicineCompany'), params.id])}"
            redirect(action: "list")
        }
    }
    // 删除多媒体内容
    def deleteMultimedia = {
        def medicineCompanyInstance = MedicineCompany.get(params.id)
        Multimedia multimediaInstance = Multimedia.get(params.mid) 

        if (medicineCompanyInstance) { 
//            medicineCompanyInstance?.multimedia?.toList().each {
//                if (it && it  == multimediaInstance) {
//                    medicineCompanyInstance?.multimedia?.remove(it) 
//                }
//            }  
            medicineCompanyInstance?.removeFromMultimedia(multimediaInstance);
            medicineCompanyInstance.save(flush:true)


            multimediaInstance = Multimedia.get(params.mid) 
            multimediaInstance.delete(flush:true)

            return redirect(action: "editgis", id: params.id)
        }
        return redirect(action: "list")
    }

    def delete = {
        def medicineCompanyInstance = MedicineCompany.get(params.id)
        if (medicineCompanyInstance) {
            try {
                medicineCompanyInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'medicineCompany.label', default: 'MedicineCompany'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'medicineCompany.label', default: 'MedicineCompany'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'medicineCompany.label', default: 'MedicineCompany'), params.id])}"
            redirect(action: "list")
        }
    }
}
